package com.example.demoweb.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.example.demoweb.dao.CustomFileRepository;
import com.example.demoweb.entity.CustomFile;
import com.example.demoweb.service.ICustomFileService;

@Service
public class CustomFileServiceImpl implements ICustomFileService{
	@Autowired
    public CustomFileRepository customFileRepository;

    @Override
    public CustomFile saveFile(CustomFile customFile) {
        return customFileRepository.save(customFile);
    }

    @Override
    public void removeFile(String id) {
        customFileRepository.delete(id);
    }

    @Override
    public CustomFile getFileById(String id) {
        return customFileRepository.findOne(id);
    }

    @Override
    public List<CustomFile> listFilesByPage(int pageIndex, int pageSize) {
        Page<CustomFile> page = null;
        List<CustomFile> list = null;

        Sort sort = new Sort(Direction.DESC,"uploadDate"); 
        Pageable pageable = new PageRequest(pageIndex, pageSize, sort);

        page = customFileRepository.findAll(pageable);
        list = page.getContent();
        return list;
    }
}
